package com.tulink.yichutu.controller.weixin;

import com.mxixm.fastboot.weixin.annotation.*;
import com.mxixm.fastboot.weixin.module.credential.WxJsTicketManager;
import com.mxixm.fastboot.weixin.module.media.WxMediaManager;
import com.mxixm.fastboot.weixin.module.message.*;
import com.mxixm.fastboot.weixin.service.WxApiService;
import com.mxixm.fastboot.weixin.service.WxExtendService;
import com.mxixm.fastboot.weixin.web.WxUserManager;
import com.tulink.yichutu.biz.LoginBiz;
import com.tulink.yichutu.common.utils.DataResult;
import com.tulink.yichutu.dto.OfflineVerifyCodeDto;
import com.tulink.yichutu.vo.resp.QrcodeRespVO;
import com.tulink.yichutu.vo.resp.WxLoginRespVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * weixin
 *
 * @author wanchaopeng
 * @date 2021/04/04 12:43
 */
@WxController
@RequestMapping("/weixin")
@RestController
@Slf4j
@Api(tags = "微信-公众号扫码关注登录")
public class GzhLoginController {

    @Autowired
    WxApiService wxApiService;

    @Autowired
    WxMediaManager wxMediaManager;

    @Autowired
    WxMessageTemplate wxMessageTemplate;

    @Autowired
    WxExtendService wxExtendService;

    @Autowired
    WxJsTicketManager wxJsTicketManager;

    @Autowired
    WxUserManager wxUserManager;

    @Autowired
    LoginBiz loginBiz;

    /**
     * 获取二维码
     * @param deviceCode
     * @return
     */
    @RequestMapping("loginQrcode")
    public DataResult<QrcodeRespVO> qrcode(String deviceCode) {
        String devies = deviceCode;
        if(StringUtils.isEmpty(devies)) {
            devies = UUID.randomUUID().toString();
        }
        String scene = UUID.randomUUID().toString();
        return new DataResult<>(loginBiz.genQrcode(scene, devies));
    }

    /**
     * 等待扫码轮询
     * @param id
     * @return
     */
    @RequestMapping("tryLogin")
    public DataResult<WxLoginRespVO> tryLogin(String id, HttpServletRequest request) {
        return new DataResult<>(loginBiz.tryLogin(id, request));
    }

    /**
     * 登录
     * @param userId
     * @param deviceCode
     * @return
     */
    @RequestMapping("login")
    public DataResult<WxLoginRespVO> login(String userId, String deviceCode, HttpServletRequest request) {
        return new DataResult<>(loginBiz.login(userId, deviceCode, request));
    }

    /**
     * 离线登录
     * @param deviceCode
     * @param verifyCode
     * @return
     */
    @RequestMapping("offlineLogin")
    public void offlineLogin(
            String deviceCode,
            String version,
            String verifyCode,
            Integer verifyCodeLength,
            HttpServletResponse response) throws IOException {
        DataResult<QrcodeRespVO> qrcodeRespVo = qrcode(deviceCode);
        OfflineVerifyCodeDto verifyCodeDto = new OfflineVerifyCodeDto();
        verifyCodeDto.setVersion(version);
        verifyCodeDto.setDeviceCode(deviceCode);
        verifyCodeDto.setVerifyCode(verifyCode);
        verifyCodeDto.setVerifyCodeLength(verifyCodeLength);
        loginBiz.offlineLogin(qrcodeRespVo.getData(), verifyCodeDto);
        //跳转到二维码页面
        response.sendRedirect(qrcodeRespVo.getData().getShowUrl());
    }
}
